# from main import app
from pydantic import BaseModel
from typing import Optional, Union
from fastapi import Header, UploadFile, APIRouter
from utils.jwt.gettoken import verify_token
import redis
# from typing import Optional
# 消息发布
from utils.sql.public.message import publish, get_messagelist, delete_message

# 用户相关
from utils.sql.pc.user import newUsers, forbidden, login, userinfo, updateuserinfo, update_pwd, get_app_users, \
    get_app_feedbacklist, userList, isadmin, deleteUsers, feedback_details, userdetails, outLogin
# 首页图表数据
from utils.sql.pc.statistics import get_new_user_increment

pc_router = APIRouter()
invalid_token = {
    "code": 205,
    "data": '',
    "message": "token have expired"
}


# 解密token并验证是否有效
async def redisToken(token):
    r = redis.Redis(host='localhost', port=6379, decode_responses=True)
    users = await verify_token(token)
    if users:
        user_id = users['data']['id']
        redis_Token = r.get(user_id)
        if redis_Token == token:
            r.expire(user_id, r.ttl(user_id) + 1800)
            return users['data']
        else:
            return False
    else:
        return False


# 登录
class Logins(BaseModel):
    account: str
    password: str


@pc_router.post('/api/manday/pc/login')
async def user_login(data: Logins):
    return await login(data)


# 退出登录
@pc_router.get('/api/manday/pc/outlogin')
async def out_login(Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return await outLogin(user_data['id'])
    else:
        return invalid_token


# 获取个人信息
@pc_router.get('/api/manday/pc/userinfo')
async def user_info(Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return userinfo(user_data['id'])
    else:
        return invalid_token


# 修改个人信息
class Update_info(BaseModel):
    name: str
    phone: str
    email: str


@pc_router.post('/api/manday/pc/updateinfo')
async def updateinfo(data: Update_info, Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return updateuserinfo(data, user_data['id'])
    else:
        return invalid_token


# 修改密码
class Update_pwd(BaseModel):
    olpassword: str
    newpassword: str


@pc_router.post('/api/manday/pc/updatepwd')
async def updatepwd(data: Update_pwd, Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return update_pwd(data, user_data['id'])
    else:
        return invalid_token


# 获取消息列表

@pc_router.get('/api/manday/pc/messagelist')
async def get_message(query: Union[int, str], pageNum: Union[int, str], pageSize: Union[int, str],
                      Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return get_messagelist(query, pageNum, pageSize)
    else:
        return invalid_token


# 发布消息与通知
class Message(BaseModel):
    msg_title: str
    msg_cont: str
    start_time: str
    end_time: str
    msg_recipient: str
    id: str


@pc_router.post('/api/manday/pc/messages')
async def post_message(data: Message, Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return publish(data)
    else:
        return invalid_token


# 删除消息
class DleteMessage(BaseModel):
    id: int


@pc_router.post('/api/manday/pc/deletemessages')
async def post_message(data: DleteMessage, Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return delete_message(data)
    else:
        return invalid_token


# 添加用户
class PcUser(BaseModel):
    account: str
    email: str
    forbid: str
    name: str
    phone: str
    role: str
    username: str
    userid: int


@pc_router.post('/api/manday/pc/addpcuser')
async def post_pcuser(data: PcUser, Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return newUsers(data)
    else:
        return invalid_token
    # return newUsers(data)


# 获取pc端用户列表
@pc_router.get('/api/manday/pc/userlist')
async def post_pcuser(query: Union[int, str], pageNum: Union[int, str], pageSize: Union[int, str],
                      Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return userList(query, pageNum, pageSize)
    else:
        return invalid_token


# 禁用用户-取消禁用
@pc_router.get('/api/manday/pc/forbiddenuser/{userid}')
async def post_forbidden(userid: Union[int, str], Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return forbidden(userid)
    else:
        return invalid_token


# 管理员设置和取消
@pc_router.get('/api/manday/pc/isadmin/{userid}')
async def post_isadmin(userid: Union[int, str], Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return isadmin(userid)
    else:
        return invalid_token


# 删除用户
@pc_router.get('/api/manday/pc/deleteuser/{userid}')
async def delete_Users(userid: Union[int, str], Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return deleteUsers(userid)
    else:
        return invalid_token


# 查询小程序用户
@pc_router.get('/api/manday/pc/appusers')
async def appuser(query: Union[int, str], pageNum: Union[int, str], pageSize: Union[int, str],
                  Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return get_app_users(query, pageNum, pageSize)
    else:
        return invalid_token


# 查询用户反馈列表
@pc_router.get('/api/manday/pc/feedback')
async def feedbacklist(query: Union[int, str], pageNum: Union[int, str], pageSize: Union[int, str],
                       Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return get_app_feedbacklist(query, pageNum, pageSize)
    else:
        return invalid_token


# 首页echarts
@pc_router.get('/api/manday/pc/newuserincrement/{day}')
async def new_user_increment(day: Union[int, str], Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return get_new_user_increment(day)
    else:
        return invalid_token


# 查询反馈详情
@pc_router.get('/api/manday/pc/feedbackdetails/{feedbackid}')
async def new_user_increment(feedbackid: Union[int, str], Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return feedback_details(feedbackid)
    else:
        return invalid_token


# 查询app用户数据详情(图表)
@pc_router.get('/api/manday/pc/userdetails')
async def new_userdetails(userid: Union[int, str], year: Union[int, str], Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return userdetails(userid, year)
    else:
        return invalid_token
